<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\DB;
use Intervention\Image\Facades\Image;
use Illuminate\Http\Request;
use App\ShopCategories;
use App\ProductCategories;
use App\Products;
use App\UserShop;
use Auth;

class ProductController extends BaseController
{
    /**
     * Create a new ProductController instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function index()
    {
        // $userId = Auth::user()->id;
        $products = Products::where('product_id', '>', 0)->paginate(15);
        return view('admin.product_list', ['products'=>$products]);
    }

    /**
     * 商品添加
     * @return [type] [description]
     */
    public function create()
    {
        // $userId = Auth::user()->id;
        // $shopId = UserShop::where('user_id', $userId)->select('shop_id')->first()->shop_id;

        $categories = ProductCategories::where('pid', 0)->get();
        return view('admin.product_add', ['categories'=>$categories]);
    }

    /**
     * 保存商品
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function store(Request $request)
    {
        // $userId = Auth::user()->id;
        // $shopId = UserShop::where('user_id', $userId)->select('shop_id')->first()->shop_id;

        $this->validate($request, [
            'product_name'   =>  'required|max:256|',
            // 'category_id'    =>  'required|max:5|',
            'product_category'  =>  'required|max:11|',
            // 'brand'          =>  'required|max:80|',
            'photo'          =>  'image|file',
            'description'    =>  'required|max:80',
            'price'          =>  'required|',
        ]);


        $data = [
            'product_name'     => $request->product_name,
            'category_id'      => $request->product_category,
            'description'      => $request->description,
            // 'brand'            => $request->brand,
            'price'            => $request->price,
            'shop_id'          => 4,
        ];

        //保存图片
        $photo = $request->file('photo');
        if($photo) {
            $photoPath = 'attachs/img/';
            $photoName = md5(time().$photo->getClientOriginalName().rand(1,999)).'.'.$photo->getClientOriginalExtension();
            $res= $photo->move($photoPath, $photoName);
            $data['photo'] = $photoName;

        }
        $createId = Products::create($data);
        if ($createId!=null) return redirect('admin/product')->with('success', '添加成功');
        return redirect('admin/product')->withErrors('success', '添加成功');
    }

    /**
     * 商品图片管理
     * @return [type] [description]
     */
    public function photoList($product_id = null)
    {
        if ((int)$product_id != $product_id) {
            return redirect('admin/product')->withErrors('非法请求');
        }

        $product = Products::find($product_id);
        if ($product == null) {
            return redirect('admin/product')->withErrors('商品不存在');
        }
        $userId = Auth::user()->id;
        $shopId = UserShop::where('user_id', $userId)->select('shop_id')->first()->shop_id;
        if ($shopId != $product->shop_id) {
            return redirect('admin/product')->withErrors('您没有权限编辑');
        }

        $photos = Products::find($product_id)->hasPhotos;

        return view('admin.product_photo_list', ['photos' => $photos]);
    }

    /**
     * 商品展示
     * @return [type] [description]
     */
    public function show()
    {
       return view('admin.product_edit');
    }

    /**
     * 编辑商品
     * @param  [type] $product_id [description]
     * @return [type]             [description]
     */
    public function edit($product_id = null)
    {
        if ((int)$product_id != $product_id) {
            return redirect('admin/product')->withErrors('非法请求');
        }

        $product = Products::find($product_id);
        if ($product == null) {
            return redirect('admin/product')->withErrors('商品不存在');
        }
        $categories = ProductCategories::where('pid', 0)->get();
        return view('admin.product_edit', ['categories'=>$categories, 'product'=>$product]);
    }

    /**
     * 更新商品详情
     * @param  Request $request [description]
     * @param  [type]  $id      [description]
     * @return [type]           [description]
     */
    public function update(Request $request, $id=null)
    {
        // $userId = Auth::user()->id;
        // $shopId = UserShop::where('user_id', $userId)->select('shop_id')->first()->shop_id;

        $this->validate($request, [
            'product_name'   =>  'required|max:256|',
            // 'category_id'    =>  'required|max:5|',
            'product_category'  =>  'required|max:5|',
            // 'brand'          =>  'required|max:80|',
            'description'    =>  'required|max:80',
            'price'          =>  'required|',
        ]);

        $data = [
            'product_name'     => $request->product_name,
            'category_id'      => $request->product_category,
            'description'      => $request->description,
            // 'brand'            => $request->brand,
            'price'            => $request->price,
        ];
        //保存图片
        $photo = $request->file('photo');
        if ($photo!=null) {
            $photoPath = 'attachs/img/';
            $photoName = md5(time().$photo->getClientOriginalName().rand(1,999)).'.'.$photo->getClientOriginalExtension();
            $res= $photo->move($photoPath, $photoName);
            $data['photo'] = $photoName;
        }
        $update = Products::where('product_id', $id)->update($data);
        if ($update!=null) return redirect('admin/product/'.$id.'/edit')->with('success', '修改成功');
        return redirect('admin/product/'.$id.'/edit')->withErrors('修改失败');
    }

    /**
     * 删除
     * @param  [type] $prod_id [description]
     * @return [type]          [description]
     */
    public function destroy($prod_id = null)
    {
        if ((int)$prod_id != $prod_id) {
            return redirect('admin/product')->withErrors('删除失败，非法请求');
        }

        $product = Products::find($prod_id);
        if ($product == null) {
            return redirect('admin/product')->withErrors('删除失败, 商品不存在');
        }
        $userId = Auth::user()->id;
        $shopId = UserShop::where('user_id', $userId)->select('shop_id')->first()->shop_id;
        if ($shopId != $product->shop_id) {
            return redirect('admin/product')->withErrors('您没有权限删除');
        }

        $oProduct = new Products();
        $delete = $oProduct->where(['product_id'=>$prod_id])->delete();
        if ($delete != null) {
            return redirect('admin/product')->with('success', '删除成功');
        }
    }

}
